package cn.yeziji.forum.dao.impl;

import cn.yeziji.forum.common.ForumPageResult;
import cn.yeziji.forum.common.Paging;
import cn.yeziji.forum.dao.base.ForumFeedbackBaseDao;
import cn.yeziji.forum.dao.intf.ForumFeedbackDao;
import cn.yeziji.forum.dao.iquery.FeedbackQuery;
import cn.yeziji.forum.dto.feedback.FeedbackDTO;
import cn.yeziji.forum.entity.ForumFeedbackEntity;
import cn.yeziji.forum.utils.JwtOperaUtils;
import cn.yeziji.forum.vo.feedback.FeedbackVO;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 反馈信息逻辑实现曾
 *
 * @author gzkemays
 * @since 2022/11/5 17:58
 */
@Repository
public class ForumFeedbackDaoImpl extends ForumFeedbackBaseDao implements ForumFeedbackDao {
  @Resource HttpServletRequest request;

  @Override
  public void commit(FeedbackDTO dto) {
    Integer userId = JwtOperaUtils.getUserIdByRequest(request);
    ForumFeedbackEntity entity = dto.convertToEntity(userId);
    mapper.insert(entity);
  }

  @Override
  public ForumPageResult<FeedbackVO> list(Paging paging) {
    int count = mapper().count(FeedbackQuery.findUserFeedbackListQuery());
    paging.total(count);
    List<FeedbackVO> feedbacks =
        mapper().listPoJos(FeedbackVO.class, FeedbackQuery.findUserFeedbackPagingQuery(paging));
    return new ForumPageResult<>(feedbacks, paging);
  }
}
